const http= require("http");
const { Result } = require('./lib/entity.js');
const util = require('./lib/util.js');
const controllerFinder =  require('./finder/controllerFinder.js');
const initContextFinder =  require('./finder/initContextFinder.js');
const filterFinder = require('./finder/filterFinder.js');
const router = require('./router/mvcRouter.js'); 
const fileUtil = require("./lib/fileUtil.js");
const path = require("path");

//const config={
//    "path":"conf/conf.json",
//};

var configEnv = {
    "hostname":"127.0.0.1",
    "port":3001,
    "initContextList":[],
	"controllerPath":"../controller",
    "initContextPath":"../init",
    "filterPath":"../filter",
	"routerPath":"./conf/router.json",
    "configPath":"./conf/conf.json"
};

function dealArgParams(paras,destEnv){
    // 接收一种种参数 一种  -d  
    if(!paras) return;
    if((/^\-d/i).test(paras)){
        
        //  -dport=123
        let rpvs = paras.substr(2);
        let rpvses = rpvs.split("=");
        if(rpvses.length >= 2){
            let key = rpvses[0].trim();
            let pvs = rpvses.splice(1);
            destEnv[key] = util.joinString(pvs,"=");
        }else{
            destEnv[rpvs] = rpvs;
        }
    }
    
}

function initProcess(argv){
    let alen = argv.length;
    
    if(alen >2){
        for(var aix =2;aix < alen;aix++){
            // 处理参数
            dealArgParams(argv[aix],configEnv);
        }
    }
    if(configEnv.configPath){
    	let configJSON = fileUtil.loadJSON(path.join(__dirname,configEnv.configPath));
		configEnv = util.merge(configEnv,configJSON);
    }
    if(alen >2){
        for(var aix =2;aix < alen;aix++){
            // 处理参数
            dealArgParams(argv[aix],configEnv);
        }
    }
    
    return new Result(true,configEnv);
};
function startServe(conf){
	controllerFinder.initConf(conf.controllerPath);
	filterFinder.initConf(conf.filterPath);
	var routerPath = path.join(__dirname,conf.routerPath);
	let routerConfig = fileUtil.loadJSON(routerPath);
	router.init(routerConfig,controllerFinder,filterFinder);
    const server = http.createServer((req,res) => {
        router.doAction(req,res);
    }
    );
    server.listen(conf.port,conf.hostname,()=>{
        console.log('服务器运行在 http://'+conf.hostname+':'+conf.port+'/');
    });
};

async function main(){
    let initResult = initProcess(process.argv);
    if(initResult.isSuccess()){
        let config = initResult.obj;
        initContextFinder.initConf(config.initContextPath);
        if(config.initContextList && config.initContextList.length){
            for(let i = 0;i<config.initContextList.length;i++ ){
                let icx = initContextFinder.findInitContext(config.initContextList[i]);
                if(!icx){
                    console.log("error init context!!"+config.initContextList[i]);
                    return;
                }
                let initResult = await icx.doInit();
                if(!initResult.isSuccess()){
                    console.log("启动失败："+initResult.obj);
                    return;
                }
            }
            
        }
       
        startServe(config);
    }else{
        console.log(initResult.obj);
    }
};
main();
